﻿using System;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Ink;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;
using SinaWeiboApp_sl.Model;
using SinaWeiboApp_sl.View;
using SinaWeiboApp_sl.Command;
using System.Windows.Navigation;
using ViewModels;
using SinaWeiboClientV1;
using System.Diagnostics;
using System.Threading;
using System.Threading.Tasks;

namespace SinaWeiboApp_sl.ViewModel
{
    public partial class LoginViewModel : ViewModelBase<LoginViewModel>
    {

        protected override System.Threading.Tasks.Task BeginInitActions()
        {
            return Task.Factory.StartNew(() => { });
        }

        protected override System.Threading.Tasks.Task EndInitActions()
        {
            return Task.Factory.StartNew(() => { });
        }
        SinaWeiboClientV1.WeiboClient _sinaWeibo;

        public LoginView View { get; set; }


        public User User
        {
            get { return _UserContainerLocator(this).Value; }
            set { _UserContainerLocator(this).SetValueAndTryNotify(value); }
        }
        #region Property User User Setup
        protected PropertyContainer<User> _User;
        protected static Func<object, PropertyContainer<User>> _UserContainerLocator =
            RegisterContainerLocator<User>(
                "User",
                model =>
                    model._User =
                        model._User
                        ??
                        new PropertyContainer<User>("User"));
        #endregion







        public string State
        {
            get { return _StateContainerLocator(this).Value; }
            set { _StateContainerLocator(this).SetValueAndTryNotify(value); }
        }
        #region Property string State Setup
        protected PropertyContainer<string> _State;
        protected static Func<object, PropertyContainer<string>> _StateContainerLocator =
            RegisterContainerLocator<string>(
                "State",
                model =>
                    model._State =
                        model._State
                        ??
                        new PropertyContainer<string>("State"));
        #endregion




        

        public ICommand TextChangedCommand { get; private set; }
        public ICommand LoginCommand { get; private set; }
        public ICommand CannelCommand { get; private set; }

        public LoginViewModel(WeiboClient sinaWeibo = null)
        {

            if (sinaWeibo == null)
            {
                sinaWeibo = new WeiboClient();
            }
            _sinaWeibo = sinaWeibo;

            User = new User();
            User.Name = "jdcbtest@sina.com";
            User.Password = "jdcbtest";
            TextChangedCommand = new DelegateCommand(TextChanged, e => true);
            LoginCommand = new DelegateCommand(LoginClick, e => true);
        }

        public void TextChanged(object e)
        {

        }

        public async void LoginClick(object e)
        {
            var username = _User.Value.Name;
            var userpasswd = _User.Value.Password;

            try
            {
                var cancelToken =  CancellationToken.None;
                State = "Login...";
                var token = await SinaWeiboClientV1.TokenHelper.CreateOAuthTokenAsync("967389259", "001379b11292669a5399131cfc5b806d", username, userpasswd, cancelToken);
                _sinaWeibo.CurrentAuthToken = token;
                var userShow = await _sinaWeibo.ExecuteUsersShowAsync(new SinaWeiboClientV1.UsersShowRequest { Id = token.ContextData["UserID"].ToString() });

                State = "Login Successfully";
                View.Content = new MainView(_sinaWeibo);             
            }
            catch (Exception ex)
            {
                State = "error:" + ex.Message;
                MessageBox.Show(ex.ToString());
            }
        }
    }
}
